package com.mcintyret.site.service.users;

import com.mcintyret.site.users.User;
import com.mcintyret.site.users.database.UsersDao;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * User: mcintyret2
 * Date: 12/04/2012
 */

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Resource
    private UsersDao usersDao;

    @Override
    @Transactional("usersTransactionManager")
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = usersDao.retrieveUser(username);
        if (user == null) {
            throw new UsernameNotFoundException("No user found with username " + username);
        }
        return user;
    }
}
